<template>
  <div>
    <el-form ref="formBizMaterial" :model="formBizMaterial" label-width="75px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
      <filter-box :item-width="350" @search="refreshFormBizMaterial(true)" @reset="onReset">
        <el-form-item label="材料名称" prop="formFilter.sysUserLoginName">
          <el-input class="filter-item" v-model="formBizMaterial.formFilter.materialTitle"
            :clearable="true" placeholder="材料名称" />
        </el-form-item>
        <el-form-item label="所属人员" prop="formFilter.personnelType">
          <el-select
            class="filter-item"
            v-model="formBizMaterial.formFilter.personnelType"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizMaterial.personnelType.impl.loading"
            @visible-change="formBizMaterial.personnelType.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizMaterial.personnelType.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属模块" prop="formFilter.moduleType">
          <el-select
            class="filter-item"
            v-model="formBizMaterial.formFilter.moduleType"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizMaterial.moduleType.impl.loading"
            @visible-change="formBizMaterial.moduleType.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizMaterial.moduleType.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="资料类型" prop="formFilter.materialType">
          <el-select
            class="filter-item"
            v-model="formBizMaterial.formFilter.materialType"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizMaterial.materialType.impl.loading"
            @visible-change="formBizMaterial.materialType.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizMaterial.materialType.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="场景类型" prop="formFilter.showName">
          <el-select
            class="filter-item"
            v-model="formBizMaterial.formFilter.sceneType"
            :clearable="true"
            placeholder=""
            filterable
            :loading="formBizMaterial.sceneType.impl.loading"
            @visible-change="formBizMaterial.sceneType.impl.onVisibleChange"
          >
            <el-option
              v-for="item in formBizMaterial.sceneType.impl.dropdownList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      class="page-table"
      :data="formBizMaterial.SysUser.impl.dataList"
      :size="defaultFormItemSize"
      @sort-change="formBizMaterial.SysUser.impl.onSortChange"
      @refresh="refreshFormBizMaterial(true)"
      :seq-config="{startIndex: ((formBizMaterial.SysUser.impl.currentPage - 1) * formBizMaterial.SysUser.impl.pageSize)}"
    >
      <el-button slot="operator" type="primary" icon="el-icon-plus" :size="defaultFormItemSize" @click="onAddRow()">新建</el-button>

      <vxe-column title="序号" type="seq" width="50px"/>
      <vxe-column title="资料类型" field="material"/>
      <vxe-column title="资料名称" field="materialTitle"/>
      <vxe-column title="所属人员" field="personnel" />
      <vxe-column title="所属模块" field="module" />
      <vxe-column title="所属场景" field="scene" />
      <vxe-column title="创建时间">
        <template slot-scope="scope">
          <span>{{formatDateByStatsType(scope.row.createTime, 'day')}}</span>
        </template>
      </vxe-column>
      <vxe-column title="操作" fixed="right" width="230px">
        <template slot-scope="scope">
          <el-button class="btn-table-edit" type="text" :size="defaultFormItemSize" @click="onEditRow(scope.row)">
            编辑
          </el-button>
          <el-button
            class="btn-table-edit"
            type="text"
            :size="defaultFormItemSize"
            @click="onDeleteRow(scope.row)"
          >
            删除
          </el-button>
        </template>
      </vxe-column>

      <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px;">
        <el-pagination
          :total="formBizMaterial.SysUser.impl.totalCount"
          :current-page="formBizMaterial.SysUser.impl.currentPage"
          :page-size="formBizMaterial.SysUser.impl.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, prev, pager, next, sizes"
          @current-change="formBizMaterial.SysUser.impl.onCurrentPageChange"
          @size-change="formBizMaterial.SysUser.impl.onPageSizeChange">
        </el-pagination>
      </el-row>
    </table-box>
  </div>
</template>

<script>
import { treeDataTranslate } from '@/utils';
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin, cachedPageChildMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import {BizMaterialController, BizSubjectController, DictionaryController} from '@/api';
import editMaterial from '@/views/biz/formEditBizMaterial';

export default {
  name: 'formBizMaterial',
  props: {
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data () {
    return {
      deptIdPath: [],
      formBizMaterial: {
        formFilter: {
          materialTitle: '',
          personnelType: null,
          moduleType: null,
          materialType: null,
          sceneType: null
        },
        formFilterCopy: {
          materialTitle: '',
          personnelType: null,
          moduleType: null,
          materialType: null,
          sceneType: null
        },
        SysUser: {
          impl: new TableWidget(this.loadBizMaterialData, this.loadBizMaterialVerify, true, false, 'createTime', 1)
        },
        isInit: false,
        personnelType:{
          impl: new DropdownWidget(this.loadPersonnelTypeDropdownList)
        },
        moduleType:{
          impl: new DropdownWidget(this.loadModuleTypeDropdownList)
        },
        materialType:{
          impl: new DropdownWidget(this.loadMaterialTypeDropdownList)
        },
        sceneType:{
          impl: new DropdownWidget(this.loadSceneTypeDropdownList)
        }
      }
    }
  },
  methods: {
    onReset () {
      this.$refs.formBizMaterial.resetFields();
      this.formBizMaterial.formFilter.materialTitle = '';
      this.refreshFormBizMaterial(true);
    },
    onAddRow () {
      this.$dialog.show('新建资料', editMaterial, {
        area: '1200px'
      }).then((res) => {
        this.refreshFormBizMaterial();
      }).catch(() => {
      });
    },
    onEditRow (row) {
      this.loadRowData(row).then(rowData => {
        this.$dialog.show('编辑资料', editMaterial, {
          area: '1200px'
        }, {
          rowData: rowData
        }).then((res) => {
          this.refreshFormBizMaterial();
        }).catch(e => {
        });
      }).catch(e => {
        //
      });
    },
    loadRowData (row) {
      return new Promise((resolve, reject) => {
        var params = {
          materialId: row.materialId
        }

        BizMaterialController.findById(this, params).then(res => {
          resolve(res.data);
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadBizMaterialData (params) {
      params.materialFilter = {
        materialTitle: this.formBizMaterial.formFilterCopy.materialTitle,
        personnelType: this.formBizMaterial.formFilterCopy.personnelType,
        moduleType: this.formBizMaterial.formFilterCopy.moduleType,
        materialType: this.formBizMaterial.formFilterCopy.materialType,
        sceneType: this.formBizMaterial.formFilterCopy.sceneType
      }
      return new Promise((resolve, reject) => {
        BizMaterialController.list(this, params).then(res => {
          resolve({
            dataList: res.data.dataList,
            totalCount: res.data.totalCount
          });
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadBizMaterialVerify () {
      this.formBizMaterial.formFilterCopy.materialTitle = this.formBizMaterial.formFilter.materialTitle;
      this.formBizMaterial.formFilterCopy.personnelType = this.formBizMaterial.formFilter.personnelType;
      this.formBizMaterial.formFilterCopy.moduleType = this.formBizMaterial.formFilter.moduleType;
      this.formBizMaterial.formFilterCopy.materialType = this.formBizMaterial.formFilter.materialType;
      this.formBizMaterial.formFilterCopy.sceneType = this.formBizMaterial.formFilter.sceneType;
      return true;
    },
    refreshFormBizMaterial (reloadData = false) {
      // 重新获取数据组件的数据
      if (reloadData) {
        this.formBizMaterial.SysUser.impl.refreshTable(true, 1);
      } else {
        this.formBizMaterial.SysUser.impl.refreshTable();
      }
      this.formBizMaterial.isInit = true;
    },
    onResume () {
      this.refreshFormBizMaterial();
    },
    initFormData () {
    },
    formInit () {
      this.initFormData();
      this.refreshFormBizMaterial();
    },
    /**
     * 人员类型下拉数据获取函数
     */
    loadPersonnelTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: "PersonnelType"
        };
        DictionaryController.dictGlobalDict(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadModuleTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: "ModuleType"
        };
        DictionaryController.dictGlobalDict(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadMaterialTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: "MaterialType"
        };
        DictionaryController.dictGlobalDict(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadSceneTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: "SceneType"
        };
        DictionaryController.dictGlobalDict(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    onDeleteRow(row) {
      const params = {
        materialId: row.materialId,
      }
      this.$confirm('是否删除资料,' + row.materialTitle + '？')
        .then(res => {
          return BizMaterialController.logicDelete(this, params);
        })
        .then(res => {
          if (res.success) {
            this.$message.success('删除成功');
            this.formInit();
          } else {
            this.$message.error(res.errorMessage);
          }
        })
        .catch(e => {
          console.error(e);
        });
    },
  },
  created () {
    this.formInit();
  }
}
</script>
